<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/novel_channelList' }">频道列表</el-breadcrumb-item>
            <el-breadcrumb-item>栏目列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-form ref="form" :model="searchForm" label-width="80px" :inline="true" size="small" class="admin_from">
                <el-form-item label="">
                    <el-form-item label=""
                                  :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                        <el-select v-model="searchForm.type" placeholder="请选择类型" clearable
                                   :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                            <el-option label="小说列表" value="0"></el-option>
                            <el-option label="广告位" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label=""
                                  :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable
                                   :class="this.$store.state.windowWidth<=this.$store.state.MaxWidth?'maxWidth':''">
                            <el-option label="禁用" value="0"></el-option>
                            <el-option label="启用" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="search">查询</el-button>
                    <el-button type="primary" size="small" @click="addRecharge">添加栏目</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        prop="list_order"
                        label="排序">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="类型">
                    <template slot-scope="scope">
                        <span v-if="scope.row.type==0">小说列表</span>
                        <span v-if="scope.row.type==1">广告位</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="channelInfo.title"
                        label="频道标题">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="标题">
                </el-table-column>
                <el-table-column
                        prop="books_num"
                        label="书本总数">
                </el-table-column>
                <el-table-column
                        width="240"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" plain size="small" @click="lookList(scope.row.id)">小说列表</el-button>
                        <el-button type="warning" plain size="small" @click="edit(scope.row.id)">修改</el-button>
                        <el-button type="danger" plain size="small" @click="remove(scope.row.id)">删除</el-button>
                    </template>

                </el-table-column>
            </el-table>
            <div class="el-pagination">
                <el-pagination
                        background
                        @size-change="changeSize"
                        @current-change="changeCurrent"
                        :current-page="params.pagenum"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="params.pagesize"
                        :total="params.totals"
                        :layout="this.$store.state.paginationLayout">
                </el-pagination>
            </div>
            <el-dialog :title="fromTitle" :width="this.$store.state.dialogWidth" :visible.sync="visible">
                <el-row :gutter="20">
                    <el-col :span="this.$store.state.elColNum">
                        <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small">
                            <el-form-item label="排序">
                                <el-input v-model="form.list_order"></el-input>
                            </el-form-item>
                            <el-form-item label="类型">
                                <el-select v-model="form.type" placeholder="选择类型">
                                    <el-option label="小说列表" value="0"></el-option>
                                    <el-option label="广告位" value="1"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="标题">
                                <el-input v-model="form.title"></el-input>
                            </el-form-item>
                            <el-form-item label="首页推荐展示数量" v-if="form.type==0">
                                <el-input v-model="form.recommend_nums"></el-input>
                            </el-form-item>
                            <!--                            <el-form-item label="描述" v-if="form.type==1">-->
                            <!--                                <el-input type="textarea" v-model="form.describe"></el-input>-->
                            <!--                            </el-form-item>-->

                            <!--                            <el-form-item label="缩略图" v-if="form.type==1">-->
                            <!--                                <el-upload-->
                            <!--                                        class="avatar-uploader"-->
                            <!--                                        action="upDataUrl"-->
                            <!--                                        :show-file-list="false"-->
                            <!--                                        :http-request="myUpload"-->
                            <!--                                        :before-upload="beforeAvatarUpload">-->
                            <!--                                    <img v-if="form.thumb" :src="form.thumb" class="avatar">-->
                            <!--                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                            <!--                                </el-upload>-->
                            <!--                            </el-form-item>-->
                            <el-form-item label="样式" v-if="form.type==0">
                                <el-radio v-model="form.style" :label="item.key" v-for="(item,index) in styleList"
                                          :key="index">{{item.value}}
                                </el-radio>
                            </el-form-item>
                            <el-form-item label="" v-if="form.type==0">
                                <img :src="item.image" alt="" v-for="(item,index) in styleList"
                                     :key="index" v-show="form.style===index" style="width: 100%">
                            </el-form-item>
                            <style2 v-if="form.style==1&&form.type==0" v-on:chooseData="chooseData"></style2>
                            <style6 v-if="form.style==5&&form.type==0" v-on:chooseData="chooseData"></style6>
                            <style7 v-if="form.style==6&&form.type==0" v-on:chooseData="chooseData"></style7>
                            <style8 v-if="form.style==7&&form.type==0" v-on:chooseData="chooseData"></style8>
                            <el-form-item label="更多" v-if="form.type==0">
                                <el-select v-model="form.more" placeholder="是否开启更多">
                                    <el-option label="开启" value="1"></el-option>
                                    <el-option label="关闭" value="0"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="状态">
                                <el-select v-model="form.status" placeholder="状态">
                                    <el-option label="启用" value="1"></el-option>
                                    <el-option label="禁用" value="0"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
                <span slot="footer" class="dialog-footer">
                <el-button @click="visible=false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirm" size="small">确 定</el-button></span>
            </el-dialog>

            <el-dialog title="小说列表" width="80%" :visible.sync="novelvisible">
                <novelList v-if="novelvisible" :columnId="columnId"></novelList>
                <span slot="footer" class="dialog-footer">
                <el-button @click="novelvisible=false" size="small">关闭</el-button>
                </span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
  import {uploadimg, isRealNum} from '../../../utils/common'
  import novelList from '../../../components/novel_channel/channel_novel_list'
  import style2 from '../../../components/column/style2'
  import style6 from '../../../components/column/style6'
  import style7 from '../../../components/column/style7'
  import style8 from '../../../components/column/style8'

  export default {
    name: "Comic_Release",
    components: {style2, style6, style7, style8, novelList},
    data() {
      return {
        params: {
          pagenum: 1,
          pagesize: 10,
          totals: 0
        },
        tableData: [],
        form: {
          title: '',
          list_order: '',
          status: '',
          thumb: '',//缩略图
          describe: '',
          type: '0',
          more: '',
          style: '',
          recommends: '',
          recommend_nums:'',
        },
        fromTitle: '新增',
        visible: false,
        id: '',
        channel_id: '',
        styleList: [],
        searchForm: {
          type: '',
          status: '',
        },
        options: [],
        value1: '',
        novelvisible: false,
        columnId: '',
      }
    },
    created() {
      this.channel_id = this.$route.query.channel_id;
      this.getList();
      this.getStyle();
      this.loadAll()
    },
    methods: {
      loadAll() {
        this.$axios.post("admin/novel/getAll", '').then((res) => {
          this.options = res.data;
          this.options.map((num) => {
            num.value = num.title;
          })
        });
      },
      addRecharge() {
        this.fromTitle = '新增';
        this.id = '';
        this.form = {
          title: '',
          list_order: '',
          status: '',
          thumb: '',//缩略图
          describe: '',
          type: '0',
          more: '',
          style: '',
          recommends: '',
          recommend_nums:'',
        };
        this.$store.dispatch('getNewNovel', this.form.recommends);
        this.visible = true;
      },
      getStyle() {
        this.$axios.post("admin/column/getStyles", '').then((res) => {
          let data = res.data;
          this.styleList = data;
        });
      },
      getList() {
        let data = {
          page: this.params.pagenum,
          page_size: this.params.pagesize,
          channel_id: this.channel_id,
          type: this.searchForm.type,
          status: this.searchForm.status,
        };
        this.$axios.post("admin/column/index", data).then((res) => {
          this.params.totals = parseInt(res.data.count);
          this.tableData = res.data.list;
        });
      },
      lookList(id) {
        this.novelvisible = true;
        this.columnId = id;
        // this.$router.push({
        //     path: '/channel_novel_list',
        //     query: {
        //         column_id: id,
        //         channel_id: this.channel_id
        //     }
        // })
      },
      // 每页显示多少条
      changeSize(val) {
        this.params.pagesize = val;
        this.getList();
      },
      // 当前显示的页数
      changeCurrent(val) {
        this.params.pagenum = val;
        this.getList()
      },
      // 编辑用户
      edit(id) {
        this.id = id;
        this.fromTitle = '编辑';
        this.$axios.get('admin/column/' + id, {}).then((res) => {
          let data = res.data;
          for (let key in data) {
            if (isRealNum(data[key]) && key !== 'style') {
              data[key] = data[key].toString()
            }
          }
          this.form = data;
          this.$store.dispatch('getNewNovel', data.recommends);
          this.visible = true;
        });
      },
      // 确认提交添加或编辑用户
      dialogAffirm() {
        if (this.id) {
          // 编辑
          this.$axios.put('admin/column/' + this.id, this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        } else {
          // 添加
          this.form = Object.assign(this.form, {channel_id: this.channel_id})
          this.$axios.post('/admin/column', this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        }
      },
      remove(id) {
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.del('admin/column/' + id, '').then((res) => {
            this.$message.success('删除成功!');
            this.getList();
          });
        }).catch(() => {

        });
      },

      beforeAvatarUpload(file) {
        var suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
        let arr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'pdf', 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'PDF']
        if (arr.indexOf(suffix) === -1) {
          this.$message.error('上传图片格式有误');
          return false
        }
      },
      // 上传图片文件
      myUpload(content) {
        uploadimg(content).then(res => {
          this.form.thumb = res;
        })
      },
      search() {
        this.params.pagenum = 1;
        this.getList();
      },
      chooseData(data) {
        this.form.recommends = data;
        console.log(data);
      }
    }
  }
</script>

<style scoped>

</style>